#include "Queue_Implement_Stack.h"

MyStack* myStackCreate() {
    MyStack* obj = (MyStack*)malloc(sizeof(MyStack));
    obj->STbottom = (Queue*)malloc(sizeof(Queue));
    obj->STtop = (Queue*)malloc(sizeof(Queue));
    QueueInit(obj->STbottom);
    QueueInit(obj->STtop);
    return obj;
}

void myStackFree(MyStack* obj) {
    QueueDestroy(obj->STbottom);
    QueueDestroy(obj->STtop);
    free(obj->STbottom);
    obj->STbottom = NULL;
    free(obj->STtop);
    obj->STtop = NULL;
    free(obj);
    obj = NULL;
}

void myStackPush(MyStack* obj, QDataType data) {
    assert(obj);
    QueuePush(obj->STbottom, data);
}

QDataType myStackPop(MyStack* obj) {
    assert(obj);
    while (obj->STbottom->_rear != obj->STbottom->_front) {
        QueuePush(obj->STtop, QueueFront(obj->STbottom));
        QueuePop(obj->STbottom);
    }
    QDataType del = obj->STbottom->_rear->_data;
    QueuePop(obj->STbottom);
    while (obj->STtop->_rear) {
        QueuePush(obj->STbottom, QueueFront(obj->STtop));
        QueuePop(obj->STtop);
    }
    return del;
}

QDataType myStackTop(MyStack* obj) {
    assert(obj);
    return QueueBack(obj->STbottom);
}

bool myStackEmpty(MyStack* obj) {
    assert(obj);
    if (QueueEmpty(obj->STbottom)) {
        return false;
    }
    else {
        return true;
    }
}